﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using GNSWebSite.Constants;
using GNSWebSite.Settings;

namespace GNSWebSite.Helpers
{
    /// <summary>
    /// Helper entity for locales
    /// </summary>
    public static class LocaleHelper
    {
        /// <summary>
        /// Returns current language identifier (from session)
        /// </summary>
        /// <returns>Current language identifier</returns>
        public static string Current()
        {
            if (HttpContext.Current.Session != null && HttpContext.Current.Session[LocaleConstants.CurretLocaleIdInSession] != null)
            {
                return (string)HttpContext.Current.Session[LocaleConstants.CurretLocaleIdInSession];
            }
            else
            {
                return LocaleSettings.DefaultLocale;
            }
        }

        /// <summary>
        /// Returns default language identifier (language, specefied as defoult in config file)
        /// </summary>
        /// <returns>Default language identifier</returns>
        public static string Default()
        {
            return LocaleSettings.DefaultLocale;
        }
        /// <summary>
        /// Sets invariant culture to the current thread
        /// </summary>
        public static void SetInvariantCulture()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
        }

        /// <summary>
        /// Sets current culture selected from user
        /// </summary>
        public static void SetCurrentCulture()
        {
            //.Substring(0, 2)
            var cultureInfo = new CultureInfo(Current());
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Current());
        }

        public static void SetCurrent(string cultureName)
        {
            HttpContext.Current.Session[LocaleConstants.CurretLocaleIdInSession] = cultureName;
        }
    }
}